#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define SIZE 1e6+2
#define endl "\n"
void solve(){
ll a,b,c,d; cin>>a>>b>>c>>d;
if(d<b){
cout<<-1<<endl;
return;
}
ll ans1,aa;
aa=a+d-b;
if(aa<c){
cout<<-1<<endl;
return;
}
ans1=(d-b)+abs(aa-c);
cout<<ans1<<endl;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll t; cin>>t;
while(t--){
solve();
}
return 0;
}
145. Binary Tree Postorder Traversal | 94. Binary Tree Inorder Traversal |
101. Symmetric Tree | 77. Combinations |
46. Permutations | 226. Invert Binary Tree |
112. Path Sum | 1556A - A Variety of Operations |
136. Single Number | 169. Majority Element |
119. Pascal's Triangle II | 409. Longest Palindrome |
1574A - Regular Bracket Sequences | 1574B - Combinatorics Homework |
1567A - Domino Disaster | 1593A - Elections |
1607A - Linear Keyboard | EQUALCOIN Equal Coins |
XOREQN Xor Equation | MAKEPAL Weird Palindrome Making |
HILLSEQ Hill Sequence | MAXBRIDGE Maximise the bridges |
WLDRPL Wildcard Replacement | 1221. Split a String in Balanced Strings |
1002. Find Common Characters | 1602A - Two Subsequences |
1555A - PizzaForces | 1607B - Odd Grasshopper |
1084A - The Fair Nut and Elevator | 1440B - Sum of Medians |